package com.service;
import com.alibaba.fastjson.JSON;
import com.dao.AsCustomsDao;
import com.entity.AsCustoms;
import com.entity.AsUser;
import com.utils.DateUtil;
import com.utils.JsonUtil;
import com.utils.Pager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.swing.plaf.basic.BasicScrollPaneUI;
import java.util.ArrayList;
import java.util.Date;

@Service
public class AsCustomsService
{
    @Resource
    private AsCustomsDao asCustomsDao;

    //根据指定的用户id或查询条件查询其所有可用客户
    public String returnAvaliableCustomsByUserIdAndName( Integer userId , String customName )
    {
        ArrayList<AsCustoms> customsList = asCustomsDao.getAvaliableCustomsByUserIdAndCustomName( userId , customName );
        String jsonStr = JsonUtil.toJsonStrWithDateTime( customsList );
        return jsonStr;
    }

    //根据指定的用户id或用户名查询其客户
    public ArrayList<AsCustoms> returnCustomsByUserIdAndName( Integer userId , String customName , Integer page , Integer size )
    {
        int index = ( page - 1 ) * size;
        int quantity = size;

        return asCustomsDao.getCustomsByUserIdAndCustomName( userId , customName ,index , quantity );
    }

    //根据指定的用户id查询其客户数量
    public Integer returnCustomsCountByUserId( Integer userId )
    {
        return asCustomsDao.getCustomsCountByUserId( userId );
    }

    //添加用户
    public String addCustom( AsCustoms custom , AsUser user , String dateTime )
    {
        //将字符串格式时间转换为date对象
        Date regDatetime = DateUtil.strToDateTime( dateTime );
        custom.setRegDatetime( regDatetime );
        custom.setAgentId( user.getId() );
        custom.setAgentName( user.getUserName() );
        custom.setAgentCode( user.getUserCode() );
        Integer resultCode = asCustomsDao.addOneCustom( custom );

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //返回1位用户的json格式
    public String returnOneCustomJsonStr( Integer id )
    {
        AsCustoms custom = asCustomsDao.getOneCustomById( id );
        String jsonStr = JSON.toJSONStringWithDateFormat( custom , "yyyy-MM-dd hh:mm:ss" );

        return jsonStr;
    }

    //更新1位客户
    public String updateCustom( AsCustoms custom )
    {
        Integer resultCode = asCustomsDao.updateOneCustom( custom );

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //停用或启用客户
    public String stopOrStartCustom( String action , Integer customId )
    {
        AsCustoms customs = new AsCustoms();
        customs.setId( customId );
        Boolean customStatus = action.equals( "startCustom" );
        customs.setCustomStatus( customStatus );
        Integer resultCode = asCustomsDao.updateOneCustom( customs );

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

}
